from django.http import HttpResponse,StreamingHttpResponse
from django.template import loader
import csv

def index(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = "attechment;filename='abc.csv'"

    writer = csv.writer(response)
    writer.writerow(['username','age','sex'])
    writer.writerow(['91wf','18','0'])

    return response

def template_csv_view(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = "attechment;filename='abc.csv'"
    context = {
        'rows':[
            ['username','age','sex','height'],
            ['91ge','19','1','191cm'],
        ]
    }
    writer = csv.writer(response)
    #我们在系统指定的模板
    template = loader.get_template('abc.txt')
    #内容按照模板规定的显示
    csv_template = template.render(context)
    response.content = csv_template
    return response

def large_view(request):
    response = StreamingHttpResponse(content_type='text/csv')
    response['Content-Disposition'] = "attechment;filename='super.csv'"
    # writer = csv.writer(response)
    # for row in range(0,1000000):
    #     writer.writerow(['Row {}'.format(row),'{}'.format(row)])
    rows = ("Row {},{}\n".format(row,row) for row in range(0,1000000))
    response.streaming_content = rows
    return response
